<?php
use common\components\helpers\Fun;
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use common\components\extenders\MenuManage;
use doumiaoduo\layer\LayerIframeWidget;
use doumiaoduo\layer\LayerAsset;
LayerAsset::register($this);

LayerIframeWidget::widget(['button'=>"['添加','取消']"]);
?>
<div class="yx-cnt-box yx-radius2">
    <div class="yx-praise-box">
        <div class="yx-search-class">
            <a href="<?=Fun::url(['customer/details','id'=>\Yii::$app->request->get('id')])?>">
                <button type="button" class="btn yx-back-list yx-radius5 yx-width110">返回上级</button>
            </a>
            <div class="yx-bgf7 yx-height108">
                <h4 class="yx-teacher-praise yx-floatl ">会员姓名：<?= $customer->name ?></h4>
                <?php if(\Yii::$app->controller->action->id == 'order'):?>
                    <h4 class="yx-teacher-praise yx-pl20 yx-floatl">当前可用奖学金：<span class="yx-color2cb4f3 yx-font16"><?= isset($account->scholarship_available)?$account->scholarship_available:0 ?></span>元</h4>
                <?php endif;?>
                <h4 class="yx-teacher-praise yx-pl20 yx-floatl">当前可用余额：<span class="yx-color2cb4f3 yx-font16"><?= isset($account->cash)?$account->cash:0 ?></span>元</h4>
                <div class="yx-clear"></div>
            </div>
            <div class="yx-class-box yx-pt18 yx-pb0">
                 <div>
                    <?= (new MenuManage(MenuManage::TOP_MENU))->detailsMenu('customer', 'add', false, ['rel' => 'dialog-show-iframe', 'class' => 'btn yx-batch-settlement yx-radius2','href'=>"javascript:;",'data'=>['orderType'=>$orderType,'customer_id'=>\Yii::$app->request->get('id'),'name'=>'添加商品']]) ?>
                 </div>
                <div class="docs-main">
                    <table id="tablep" class="tablesaw table yx-table1 yx-table3 table-striped with-check" style="margin-bottom: 50px;"  data-url="<?=Fun::url(['customer/add'])?>" >
                        <thead>
                        <tr class="yx-bgd2f6ec">
                            <th></th>
                            <th>类别名称</th>
                            <th>商品名称</th>
                            <th>商品类型</th>
                            <th>库存总量</th>
                            <th>单节课次价格</th>
                            <th>教师名称</th>
                            <th>教室</th>
                            <th>上课时间</th>
                            <th>满额人数</th>
                            <th class="yx-br">开班人数</th>
                            <th>是否组合艺教学</th>
                        </tr>
                        </thead>

                    </table>
                    <div class="price_list">
                        <div style="float: left; margin-bottom:10px;">
                            <span class="zx-label yx-mr25" style="width: auto !important">选择价格套餐</span> <?=Html::dropDownList('goods_price_id',null,[],['class'=>'form-control','style'=>'width:510px','id'=>'goods_price_id','prompt'=>'请选择...','data-thishidden'=>"selectgoodsprice",'data-otherhidden'=>'selectartprice','data-act'=>'orders-goods_price_id'])?>
                            <?=Html::hiddenInput('goods_hidden',0,['id'=>'selectgoodsprice'])?>
                        </div>

                       <div class="yx-clear"></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="yx-class-box">
            <?php $form = ActiveForm::begin(['enableAjaxValidation' => true, 'enableClientValidation' => false,'options'=>['class'=>'form-validate form-horizontal']]); ?>
            <div class="control-group" style="display: none;margin-bottom:0;">
                <label class="control-label control-label1 yx-mr25" for="inputTime"><?= $model->attributeLabels()['total_amount']; ?></label>
                <div class="controls">
                    <?= $form->field($model, 'total_amount', ['template' => '{input}{error}'])->hiddenInput(); ?>
                </div>
            </div>

            <div class="control-group" style="display: none;margin-bottom:0;">
                <label class="control-label control-label1 yx-mr25" for="inputTime"><?= $model->attributeLabels()['item_num']; ?></label>
                <div class="controls">
                    <?= $form->field($model, 'item_num', ['template' => '{input}{error}'])->hiddenInput(); ?>
                </div>
            </div>
            <div class="control-group" style="margin-bottom:0;">
                <label class="control-label control-label1 yx-mr25" for="inputTime">合计金额</label>
                <div class="controls">
                    <span class="yx-font18 yx-color4f4f4f" style="line-height:30px;" id="amount">0.00</span>
                </div>
            </div>
             <div class="control-group" style="margin-bottom:0;">
                    <label class="control-label control-label1 yx-mr25" for="inputTime"><?= $model->attributeLabels()['final_amount']; ?></label>
                    <div class="controls">
                        <?= $form->field($model, 'final_amount', ['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:220px', 'readonly' => 'true', 'value' => 0, 'class' => 'input-text']) ?>
                    </div>
                </div>

            <div class="control-group" style="margin-bottom:0;">
                <label class="control-label control-label1 yx-mr25" for="inputTime"><?= $model->attributeLabels()['goods_id']; ?></label>
                <div class="controls">
                    <?= $form->field($model, 'goods_id', ['template' => '{input}{error}'])->textInput(['style' => 'width:220px', 'class' => 'input-text','readonly'=>true]); ?>
                </div>
            </div>
            <div class="control-group"  style="display: none;margin-bottom:0;">
                <label class="control-label control-label1 yx-mr25" for="inputTime"><?= $model->attributeLabels()['goods_price_id']; ?></label>
                <div class="controls">
                    <?= $form->field($model, 'goods_price_id', ['template' => '{input}{error}'])->textInput(['style' => 'width:220px', 'class' => 'input-text','readonly'=>true]); ?>
                </div>
            </div>
            <div class="control-group" style="margin-bottom:0;">
                <label class="control-label control-label1 yx-mr25" for="inputTime"><?= $model->attributeLabels()['remarks']; ?></label>
                <div class="controls">
                    <?= $form->field($model, 'remarks', ['template' => '{input}{error}{hint}'])->textarea(['rows' => 2, 'class' => 'textarea_editor yx-width510 yx-height56']) ?>
                </div>
            </div>
                <div class="yx-button-group">
                    <button type="button" id="dosubmit" class="btn yx-save yx-radius50">提交</button>
                    <a href="<?= $url['back'] ?>" class="btn yx-close1 yx-radius50 yx-ml4" style="display: inline-block">返回</a>
                </div>
            <?php ActiveForm::end(); ?>
        </div>

    </div>
</div>
<?php
$js=<<<JS

   //添加商品
   $(document).on('click','.layui-layer-btn0',function(e){
         //e.preventDefault();
         var price =0;
         var input = $("iframe[id^=layui-layer-iframe]").contents().find('input[name="checkbox"]:checked');
         var tr = input.parents('tr');
         var id = input.val();
         var str = '';
         var url = $('#tablep').data('url');
         if(typeof(id) === 'undefined'){
           layer.msg("请先选择商品！",{id:'layer-msg',skin: 'layui-msg-customskin',time: 1000});
           return false;
         }else{
            
            $.ajax({
             url:url,
             type:'post',
             data:{id:id,ajax:'goodsprice',_csrf:yii.getCsrfToken()},
             dataType:'json',
             beforeSend:function(){
                layer.load(0, { shade: [0.5, '#393D49'] });
             },
             complete:function(){
              layer.close(layer.load(0, { shade: [0.5, '#393D49'] }));  
            },
            success:function(data){
               if(data.status == 1){
                   var str = '<option value="">请选择...</option>';
                   var pricelist = data.priceList;
                   for(var i= 0 ;i<pricelist.length;i++){
                       var item = pricelist[i];
                       str += '<option value="'+item.id+'" data-price="'+item.lesson_price+'">'+item.price_name+item.activity_name+'-'+item.lesson+'节课-'+item.lesson_price+'元-'+item.scholarship_desc+'</option>';
                   }
                   $('#goods_price_id').html(str);
                   // var artlist = data.artList;
                   // if(artlist.length > 0){
                   //     var html = '<span class="zx-label" style="width: auto !important">选择艺教学:</span> ';
                   //         html += '<select name="art_teaching_id" class="form-control" style="width:300px" data-thishidden="selectartprice" data-otherhidden="selectgoodsprice" data-act="orders-art_id">';
                   //         html += '<option value="">请选择...</option>'
                   //         var artlist = data.artList;
                   //         for(var i= 0 ;i<artlist.length;i++){
                   //             var items = artlist[i];
                   //             html += '<option value="'+items.id+'" data-price="'+items.price+'">'+items.title+'</option>';
                   //         }
                   //         html += '</select>';
                   //         $('#art_teaching').html(html);
                   // }
               }
            }
         });
             
              str += '<tr>';
              str += tr.html();
              str += '</tr>';
              price = parseInt(tr.find('td').eq(5).html());
              //price = parseInt(tr.find('td').eq(5).html());
              $('tbody').html('');             
             $('#orders-goods_id').val(id);
             $('.tablesaw').append(str);
             
             $(".layui-layer-btn1").click();
         }
         // if($('.tablesaw input[name="checkbox"]').length > 0){
         //     $('input[name="checkbox"]').each(function(){
         //         var parents = $(this).parents('tr');
         //          hereids.push($(this).val());
         //          oldprice = parseInt(parents.find('td').eq(5).html())+oldprice;
         //       });
         //  }
         // $("iframe[id^=layui-layer-iframe]").contents().find('input[name="checkbox"]:checked').each(function(){
         //     var parent = $(this).parents('tr');
         //     var _this = $(this);
         //        if($.inArray(_this.val(),hereids) == -1){
         //           str += '<tr>';
         //           str += parent.html();
         //           str += '</tr>';
         //           newprice = parseInt(parent.find('td').eq(5).html())+newprice;
         //           nids.push(_this.val());
         //        }
         //       ids.push(_this.val());
         // });
         // if(ids.length == 0){
         //   layer.msg("请先选择商品！",{id:'layer-msg',skin: 'layui-msg-customskin',time: 1000});
         //   return false;
         //   }else{
         //    var newarr =  $.merge(nids,hereids);
         //    $('#amount').html(oldprice+newprice);
         //    $('#orders-final_amount').val(oldprice+newprice);
         //    $('#orders-total_amount').val(oldprice+newprice);
         //    $('#orders-goods_id').val(newarr);
         //    $('#orders-item_num').val(newarr.length);
         //    $('.tablesaw').append(str);
         //    $(".layui-layer-btn1").click();
         // }
         // return false;
   });

 $(document).on('click','#dosubmit',function() {
      var goods_price_id = $('select[name="goods_price_id"]').val();
      if(goods_price_id.length == 0){
          layer.msg("请选择先添加商品并选择价格！",{id:'layer-msg',skin: 'layui-msg-customskin',time: 2000});
          return false;
      }else{
          $('#goods_price_id').val('');
          $('#w0').submit();
      }
 });
 
 $(document).on('change','select',function() {
     var id = $(this).val();
     var act = $(this).data('act'); 
     //if(act === 'orders-goods_price_id'){
     var price = parseFloat($(this).find('option:selected').data('price'));
     $('#orders-final_amount').val(price);
     $('#orders-total_amount').val(price);
     $('#amount').html(price);
    // }
     $('#'+act).val(id);
     
     
 })

   //移除商品
   $(document).on('click','#remove',function(){
        var hereids = [],ids = [], newprice = 0;
        if($('.tablesaw input[name="checkbox"]').length > 0){
             $('input[name="checkbox"]').each(function(){
                var parents = $(this).parents('tr');
               if($(this).is(':checked')){
                    hereids.push($(this).val());
                    parents.remove();
                 }else{
                     ids.push($(this).val());
                     newprice = parseInt(parents.find('td').eq(5).html())+newprice;
                 }
               });
               if(hereids.length == 0){
                  layer.msg("请先选择要移除的商品！",{id:'layer-msg',skin: 'layui-msg-customskin',time: 1000});
                  return false;
               }
               $('#amount').html(newprice);
               $('#orders-final_amount').val(newprice);
               $('#orders-total_amount').val(newprice);
               $('#orders-goods_id').val(ids);
               $('#orders-item_num').val(ids.length);
          }
   });


JS;
\Yii::$app->view->registerJs($js);
?>